// 计划详情 /safe/api/safePlanCourse/detail/${计划id}

import { tradeGroupResult } from '@/components/Trade/utils';
import { groupBy } from 'lodash-es';
import { request } from '../request';
import { AddSource, CycleField, FixedField, StudyObject, StudyPlan } from './interface';

export type PlanType = StudyPlan & CycleField & StudyObject & FixedField & AddSource;

export interface StudyPlanListSearchParams {
  name: string;
  studyObject: string | string[];
  entId: string;
}

export interface StudyPlanListItem {
  classType: string;
  cycleTime?: number;
  effectiveTime?: string;
  entTime?: string;
  modifyTime: string;
  name: string;
  objectType: number;
  openStatus: number;
  planCourseId: string;
  serviceNumType?: unknown;
  startTime: string;
  status: number;
  studyObject: string;
  type: string;
  isCharge?: number;
  isNewInterface: number;
  studyMethod?: number;
  personNum?: number;
  isAllPerson?: number;
}

/**
 * @desc 分页查询计划列表
 */
async function secPlanList(
  { name, studyObject, entId }: Partial<StudyPlanListSearchParams>,
  pageIndex = 1,
  pageSize = 10,
) {
  const { data } = await request<{ list: StudyPlanListItem[]; total: number }>({
    url: '/safe/api/safePlanCourse/unionList',
    params: {
      pageNum: pageIndex,
      pageSize: pageSize,
      name,
      entId,
      studyObject: [studyObject].flat().join(','),
    },
    noAutoShowErrorMessage: true,
  });

  return data;
}

type PlanDetailInfo = StudyPlan & Record<string, unknown>;
/**
 * @desc 获取计划详情
 * @param type 100 固定时间学习 200 周期循环学习
 */
async function getPlanDetail(planId: string, type = '100') {
  const { data } = await request<PlanDetailInfo>({
    url: `/safe/api/${type === '100' ? 'safePlanTask' : 'safePlanCourse'}/detail/${planId}`,
  });
  return data;
}

export type PlanTempItem = {
  guid: string;
  region: string;
  entTypeCode: string;
  trade: string;
  name: string;
  /**
   * @desc 学习对象
   */
  learnObj: string;
  creatTime: string;
  learnMode: string;
  cycleType: string;
  cycleDay: string;
  faceImg: string;
} & Record<string, unknown>;

/**
 * @desc 查询boss发布的计划模版列表
 */
async function secPlanTempList(params: any, pageIndex = 1, pageSize = 10) {
  const { data } = await request<{ total: number; list: PlanTempItem[] }>({
    url: '/safe/api/entPlatform/planMouldList',
    params: {
      ...params,
      pageNum: pageIndex,
      pageSize: pageSize,
    },
  });
  return data;
}

export type PlanTempContent = {
  coursewareId: string;
  coursewareType: string;
  name: string;
  pictextContent: string;
  createTime: string;
  updateTime: string;
  isDel: string;
  sortNum: number;
  coursewareSortNum: number;
} & Record<string, unknown>;
/**
 * @description 单独获取计划模版里的内容信息,获取平台课件包里面的课件+试卷列表
 * @path /safe/api/industry/courseware/listWithRelationType
 */
async function getPlanTemp(params: any) {
  const { data } = await request<{ total: number; list: PlanTempContent[] }>({
    url: '/safe/api/industry/courseware/listWithRelationType',
    params: params,
  });
  return Object.values(
    groupBy(data.list, (value) => {
      return value.sortNum;
    }),
  );
}

/**
 *@description ,获取企业协会课件包里面的课件+试卷列表
 */
export const getGCPackageContent = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/entCourseware/govAssemblePackageDetail',
    params,
  });

/**
 * @description 获取平台课件包里面的课件+试卷列表
 */
export const getPlatPackageContent = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/industry/courseware/listWithRelationType',
    params,
  });

/**
 * @description 获取分組的行業、
 */
async function getIndustryList(params: any) {
  const { data } = await request<{ total: number; list: tradeGroupResult[] }>({
    url: '/account/ent/api/typeConfig/list',
    params: params,
  });
  return data;
}

/**
 * @description 获取当前位置信息
 */

export const getCurrLocation = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanTask/newestPosition',
    params,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  });

export { getOrgnList } from './extra';
export { getIndustryList, getPlanDetail, getPlanTemp, secPlanList, secPlanTempList };

/**
 * @description 添加学习计划-固定时间
 */
export const createFixedStudyPlan = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanTask/add',
    data,
  });

/**
 * NOTE: 新接口
 *
 * @description 添加学习计划-固定时间
 */
export const createFixedStudyPlanNew = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanTask/newAdd',
    data,
  });

/**
 * @description 添加学习计划-循环
 */
export const createCircleStudyPlan = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanCourse/save',
    data,
  });

/**
 * NOTE: 新接口
 * @description 添加学习计划-循环
 */
export const createCircleStudyPlanNew = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanCourse/newSave',
    data,
  });

/***
 * @description 获取学习计划详情-固定
 */
export const getFixedStudyPlanDetail = (planCourseId: string) =>
  request({
    method: 'post',
    url: `/safe/api/safePlanTask/detail/${planCourseId}`,
  });

/***
 * @description 获取学习计划详情-循环
 */
export const getCircleStudyPlanDetail = (planCourseId: string) =>
  request({
    method: 'post',
    url: `/safe/api/safePlanCourse/detail/${planCourseId}`,
  });

/**
 * @description 更改学习计划状态--循环
 */
export const switchCircleStudyPlanStatus = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanCourse/updateCourseOpenStatus',
    params,
  });

/**
 * @description 更新学习计划生效时间--循环
 *
 */
export const updateCircleStudyPlanEffectiveTime = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanCourse/updateEffectiveTime',
    params,
  });

/**
 * @description 更改学习计划状态--固定
 */
export const switchFixedStudyPlanStatus = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanTask/updateTaskOpenStatus',
    params,
  });

/**
 * @description 删除学习计划--循环
 */
export const deleteCircleStudyPlan = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanCourse/delEntCourse',
    params,
  });

/**
 * @description 删除学习计划--固定
 */
export const deleteFixedStudyPlan = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanTask/remove',
    params,
  });

export interface TemplateTableRecord {
  guid: string;
  creatTime: string;
  updateTime?: string;
  cycleDay: string;
  cycleType: 'fixed' | 'cycle';
  faceImg: string;
  isCharge: number;
  name: string;
  trade: string;
  learnMode: string;
}

/**
 * @description 获取计划模板列表
 */
export const getStudyPlanTemplateList = async (params: any, pageIndex = 1, pageSize = 10) => {
  const { data } = await request<{ total: number; list: TemplateTableRecord[] }>({
    method: 'post',
    url: '/safe/api/entPlatform/planMouldList',
    params: {
      ...params,
      pageNum: pageIndex,
      pageSize: pageSize,
    },
  });
  return data;
};

/**
 * @description 获取学习包下课件+试卷
 * course：课程 package：包 learn：学习计划
 */

export const getStudyPackageIncludes = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/industry/courseware/listWithRelationType',
    params,
  });

/**
 * @description 企业协会课件详情
 */
export const getGovernmentCourseWareDetail = (courseWareId: string) =>
  request({
    method: 'post',
    url: `/safe/api/entCourseware/detail/${courseWareId}`,
  });

/**
 * @description 企业协会试卷详情
 */
export const getGovernmentTestPaperDetail = (courseWareId: string) =>
  request({
    method: 'post',
    url: `/safe/api/safePaper/review/${courseWareId}`,
  });

/**
 * @description 企业协会学习计划列表里的课件详情
 */
export const getGovernmentCourseWareDetailForPlan = (courseWareId: string) =>
  request({
    method: 'post',
    url: `/safe/api/entCourseware/planCoursewareDetail/${courseWareId}`,
  });

/**
 * @description 企业协会学习计划列表里的试卷详情
 */
export const getGovernmentTestPaperDetailForPlan = (courseWareId: string) =>
  request({
    method: 'post',
    url: `/safe/api/entPaper/review/${courseWareId}`,
  });

/**
 * @description 判断当前企业是否购买学习位
 */
export const hasPayServiceApi = (params: any) =>
  request({
    method: 'post',
    url: '/safe/api/safeFee/buyService/haveService',
    params,
  });
